package com.itheima.Controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empServiceimpl;

    /**
     * 条件查询员工
     *
     * @param queryParam
     * @return
     */
    @GetMapping            //    spring会自动将简单参数的数据转换成JavaBean对象;     1.接收参数
    public Result page(EmpQueryParam queryParam) {
//       2.调用Service层方法
        PageBean pageBean = empServiceimpl.page(queryParam);
//       3.响应Result数据
        return Result.success(pageBean);
    }


    /**
     * 新增员工
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp) {

        empServiceimpl.add(emp);


        return Result.success();
    }


    /**
     * 删除员工
     */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("根据id删除员工，id={}", ids);

        empServiceimpl.delete(ids);
        return Result.success();
    }


    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
//        log.info("根据id查询员工，id={}", id);

        Emp emp = empServiceimpl.getInfo(id);

        return Result.success(emp);
    }


    /**
     * 修改员工信息
     *
     * @param emp
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息，emp={}", emp);

        empServiceimpl.update(emp);

        return Result.success();
    }


    /**
     * 查询全部员工
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    public Result selectAll() {

        List<Emp> list = empServiceimpl.selectAll();

        return Result.success(list);
    }


    /**
     * 修改密码
     */
    @PutMapping("/changePassword")
    public Result changePassword(@RequestBody Password password) {
        //        1.接收参数

        //        2.调用Service层
        empServiceimpl.changePassword(password.getPassword(), password.getRepassword(), password.getNewpassword());
        //        3.返回Result
        return Result.success();
    }
}
